/**
 * 
 * @class NG.response.GetInfoEx
 * @extends Ext.util.Observable
 */

NG.command.GetInfoEx = Ext.extend(NG.command.Request, {
	
	SRID: null,
	proj: null,
	/**
	 * requires FAD list
	 * @type string[]
	 * @see NCDDServices:getResultTableList
	 */
	FAD: [],
	clockwise: null,
	countOnly: null,
	
	/**
	 * If queried table is geo table it must be true else (ie. Status table) false
	 * @cfg {Boolean} geoTable 
	 */
	geoTable: true,
	
	intersects:  {	
		propertyName: null,
		point: { coordinates: null },
		polygon: { coordinates: null },
		lineString: { coordinates: null }
	},
	
	isBetween: {
		propertyName: null,
		lowerBoundary: null,
		upperBoundary: null
	},

	isLessThan: {
		propertyName: null,
		propertyLiteral: null
	},
	
	isLike: {
		propertyName: null,
		propertyLiteral: null
	},
	
	/**
	 * Used for obtaining xml data <br>
	 * requesting detailed info about FADs according to class members
	 * 
	 * @param {Boolean} dontRefresh default is false. If this is true, buffered xml data is returned.
	 * @return {String} formed xml structure
	 */
	getDataXML: function(dontRefresh){
		if (Ext.isEmpty(dontRefresh))
			dontRefresh = false;
	
		if (dontRefresh && !Ext.isEmpty(this.getDataXMLBuffer()))
			return this.getDataXMLBuffer();
		
    	var XML = new XMLWriter();
    	
    	var FADTableList = this.obtainTableNamesFromFADList(this.FAD);
    	
    	XML.BeginNode(NG.constants.GetInfoEx.XML_GetInfoEx);
    	XML.Attrib(NG.constants.GetInfoEx.XML_GetInfoExVersionAttr, NG.constants.GetInfoEx.XML_GetInfoExVersionAttrValue);
    	for (var iFAD=0; iFAD<FADTableList.length; ++iFAD) {
    		if (this.FAD[iFAD].isGeoTable && !this.FAD[iFAD].isGeoTable())
    			continue;
		    XML.BeginNode(NG.constants.GetInfoEx.XML_GetInfo);
		    	XML.Node(NG.constants.GetInfoEx.XML_SRID, this.SRID);
		    	XML.Node(NG.constants.GetInfoEx.XML_Proj, this.proj);
		    	XML.Node(NG.constants.GetInfoEx.XML_Clockwise, this.clockwise);
		    	XML.Node(NG.constants.GetInfoEx.XML_CountOnly, this.countOnly);
		    	XML.Node(NG.constants.GetInfoEx.XML_FAD, FADTableList[iFAD]);
			    if (!this.geoTable) {
					XML.EndNode();			    	
			    	break;
			    }
		    	XML.BeginNode(NG.constants.GetInfoEx.XML_Filter);
		    	XML.Attrib(NG.constants.GetInfoEx.XML_FilterAttr, NG.constants.GetInfoEx.XML_FilterAttrValue);
		    		XML.BeginNode(NG.constants.GetInfoEx.XML_And);
		    		
	    			if (!Ext.isEmpty(this.intersects) && !Ext.isEmpty(this.intersects.propertyName))
	    			{
	    				XML.BeginNode(NG.constants.GetInfoEx.XML_PropertyIntersects);
	    					XML.Node(NG.constants.GetInfoEx.XML_PropertyName, this.intersects.propertyName);
	
						if (!Ext.isEmpty(this.intersects.point))
						{
	    					XML.BeginNode(NG.constants.GetInfoEx.XML_Point);
	    					XML.Attrib(NG.constants.GetInfoEx.XML_SRSName, NG.constants.GetInfoEx.XML_SRSValue);
	    						XML.BeginNode(NG.constants.GetInfoEx.XML_OuterBoundaryIs);
	    							if (Ext.isString(this.intersects.point.coordinates)) {
		    							XML.Node(NG.constants.GetInfoEx.XML_Coordinates, this.intersects.point.coordinates);
	    							} else {
	    								XML.Node(NG.constants.GetInfoEx.XML_Coordinates, this.intersects.point.toString());
	    							}
	    						XML.EndNode();
							XML.EndNode();
						}
						
	    				if (!Ext.isEmpty(this.intersects.polygon))
						{
	    					XML.BeginNode(NG.constants.GetInfoEx.XML_Polygon);
	    					XML.Attrib(NG.constants.GetInfoEx.XML_SRSName, NG.constants.GetInfoEx.XML_SRSValue);
		    					XML.BeginNode(NG.constants.GetInfoEx.XML_OuterBoundaryIs);
	//	    						XML.BeginNode(NG.constants.GetInfoEx.XML_LinearRing);
		    						if (Ext.isString(this.intersects.polygon.coordinates)) {
		    							XML.Node(NG.constants.GetInfoEx.XML_Coordinates, this.intersects.polygon.coordinates);
		    						} else {
		    							XML.Node(NG.constants.GetInfoEx.XML_Coordinates, this.intersects.polygon.toString());
		    						}
	//								XML.EndNode();
								XML.EndNode();
							XML.EndNode();
						}
	    				
	    				if (!Ext.isEmpty(this.intersects.lineString))
						{
	    					XML.BeginNode(NG.constants.GetInfoEx.XML_LineString);
	    					XML.Attrib(NG.constants.GetInfoEx.XML_SRSName, NG.constants.GetInfoEx.XML_SRSValue);
	    						XML.BeginNode(NG.constants.GetInfoEx.XML_OuterBoundaryIs);
	    						if (Ext.isString(this.intersects.lineString.coordinates)) {
	    							XML.Node(NG.constants.GetInfoEx.XML_Pos, this.intersects.lineString.coordinates);
	    						} else {
	    							XML.Node(NG.constants.GetInfoEx.XML_Pos, this.intersects.lineString.toString());
	    						}
	    						XML.EndNode();
							XML.EndNode();
						}
						XML.EndNode();
	    			}
	    			
	    			if (!Ext.isEmpty(this.isBetween) && !Ext.isEmpty(this.isBetween.propertyName))
					{
	    				XML.BeginNode(NG.constants.GetInfoEx.XML_PropertyIsBetween);
	    					XML.Node(NG.constants.GetInfoEx.XML_PropertyName, this.isBetween.propertyName);
	    					
	    					XML.BeginNode(NG.constants.GetInfoEx.XML_LowerBoundary);
	    						XML.Node(NG.constants.GetInfoEx.XML_Literal, this.isBetween.lowerBoundary);
							XML.EndNode();
							
							XML.BeginNode(NG.constants.GetInfoEx.XML_UpperBoundary);
								XML.Node(NG.constants.GetInfoEx.XML_Literal, this.isBetween.upperBoundary);
							XML.EndNode();
						XML.EndNode();
					}
	    			
	    			if (!Ext.isEmpty(this.isLessThan) && !Ext.isEmpty(this.isLessThan.propertyName))
					{
	    				XML.BeginNode(NG.constants.GetInfoEx.XML_PropertyIsLessThan);
	    					XML.Node(NG.constants.GetInfoEx.XML_PropertyName, this.isLessThan.propertyName);
	    					XML.Node(NG.constants.GetInfoEx.XML_Literal, this.isLessThan.propertyLiteral);
						XML.EndNode();
					}

	    			if (!Ext.isEmpty(this.isLike) && !Ext.isEmpty(this.isLike.propertyName)) {
	    				XML.BeginNode(NG.constants.GetInfoEx.XML_PropertyIsLike.nodeName);
	    		    	XML.Attrib("escapeChar", NG.constants.GetInfoEx.XML_PropertyIsLike.escapeChar);
	    		    	XML.Attrib("singleChar", NG.constants.GetInfoEx.XML_PropertyIsLike.singleChar);
	    		    	XML.Attrib("wildCard", NG.constants.GetInfoEx.XML_PropertyIsLike.wildCard);
	    					XML.Node(NG.constants.GetInfoEx.XML_PropertyName, this.isLike.propertyName);
	    					XML.Node(NG.constants.GetInfoEx.XML_Literal, this.isLike.propertyLiteral);
						XML.EndNode();
					}
	    			
				XML.EndNode();
			XML.EndNode();
			XML.EndNode();
	    }
		XML.EndNode();
		
		var dataXML = XML.ToString();
		this.setDataXMLBufer(dataXML);
    	return dataXML;
	},
	
	/**
	 * Used for getting table names, held by FADs
	 * 
	 * @param FADs 
	 * @returns {Array} Table names 
	 */
	obtainTableNamesFromFADList: function(FADs) {
		var FADTableNames = [];
		if (!Ext.isEmpty(FADs[0].data)) {
			
			for (var i=0; i<FADs.length; ++i) {
				FADTableNames.push(FADs[i].data.tableName);	
			}
		}
		else {
			FADTableNames = [FADs];
		}

		return FADTableNames;
	},
	
	/**
	 * Each request class has a response <br>
	 * Response object can be obtained over related request object
	 * 
	 * @param {String} xmlData - unparsed response data according to request
	 * return {NG.response.GetInfoEx} response object related with request
	 */
	getResponse: function(xmlData){
		return new NG.response.GetInfoEx({
			xmlData:xmlData
			//xmlData: tt//tmpgie //TODO test için daha sonra kaldır
		});
	},
	
	constructor: function(config){
		config = config || {};
		config.serviceName = XSN_NETCAD_GETINFOEX;
		NG.command.GetMap.superclass.constructor.call(this, config);
	}
	
});